package cn.tedu.springboot01.controller;

import cn.tedu.springboot01.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/demo")
public class DemoController {

    Logger logger = LoggerFactory.getLogger(DemoController.class);

    @GetMapping("/hello")
    public String hello() {
        logger.debug("Call hello()");
        return "Hello World!";
    }

    @RequestMapping("/getUser")
    //@ResponseBody //作用是将JavaBean转换成浏览器所能解析的json数据
    public User getUser() {
        User user = new User(1, "zhang", "123", "ADMIN");
        return user;
    }

    /**
     * //@RequestParam作用是进行请求参数映射的
     * 当请求参数的参数名称和控制器方法上声明的形参参数的名称一致的话，@RequestParam注解可以省略不写
     * 当请求参数的参数名称和控制器方法上声明的形参参数的名称不一致的话，@RequestParam注解不可以省略，
     * 并且在@RequestParam注解中指定请求参数的参数名才可以实现数据的映射，例如@RequestParam("aaa")
     */
    //@RequestMapping(method = RequestMethod.GET)
    @GetMapping("/getParam")
    //@ResponseBody
    public String getParam(@RequestParam String username, String password,
                           @RequestParam("aaa") String roles) {
        System.out.println(username + ":" + password + ":" + roles);
        return "get ok";
    }

    @PostMapping("/postParam")
    //@ResponseBody
    public String postParam(String username, String password, String roles) {
        System.out.println(username + ":" + password + ":" + roles);
        return "post ok";
    }

    /**
     * @param id
     * @return
     * @PathVariable作用是：将请求路径中的所拼接的数据，动态的映射到控制器方法中的形参上
     */
    @GetMapping("/user/{uid}")
    //@ResponseBody
    public User getUserById(@PathVariable("uid") Integer id) {
        return new User(id, "zhang", "123", "USER");
    }

    /**
     * @param user
     * @return
     * @RequestBody作用是：将请求体中携带的json数据转换成Java Bean
     */
    @PostMapping("/register")
    public String register(@RequestBody User user) {
        System.out.println(user.getUsername());
        return "register ok";
    }

}
